package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.bean.ShoppingCart;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.stereotype.Service;

/**
 * Created by IntelliJ IDEA.
 * User: 许志民
 * Date: 2021-11-28
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
    @Override
    public boolean sub(ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> warpper = new LambdaQueryWrapper<>();
        warpper.eq(ShoppingCart::getUserId, shoppingCart.getUserId());
        //查询购物车中对应的信息
        //查询是菜品还是套餐
        if (shoppingCart.getDishId() != null) {
            warpper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        }
        if (shoppingCart.getSetmealId() != null) {
            warpper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart shoppingCartOne = getOne(warpper);
        //查询数量如果是最后一份则直接删除否则数量减一
        LambdaUpdateWrapper<ShoppingCart> updateWrapper = new LambdaUpdateWrapper<>();
        Integer number = shoppingCartOne.getNumber();
        //如果数量在一份以上则减一
        if (number > 1) {
            shoppingCartOne.setNumber(shoppingCartOne.getNumber() - 1);
            updateById(shoppingCartOne);
            return true;
        }
        //如果只剩一份则直接删除
        if (number == 1) {
            removeById(shoppingCartOne.getId());
            return true;
        }
        return false;
    }
}
